package com.jvtd.eyl.ui.main.my.browse;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;

import com.jvtd.eyl.R;
import com.jvtd.eyl.base.BaseMvpActivity;
import com.jvtd.eyl.bean.binding.BrowseBean;
import com.jvtd.eyl.databinding.ActivityBrowseBinding;
import com.jvtd.eyl.ui.main.my.MyMvpView;
import com.jvtd.eyl.ui.main.my.MyPresenter;
import com.jvtd.utils.SmartRefreshUtils;
import com.jvtd.widget.emptyView.JvtdEmptyView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import javax.inject.Inject;

/**
 * 我的预览
 * 作者:chenlei
 * 时间:2018/11/14 4:01 PM
 */
public class BrowseActivity extends BaseMvpActivity implements MyMvpView, OnRefreshListener {
    @Inject
    MyPresenter<MyMvpView> mPresenter;
    private ActivityBrowseBinding mBinding;

    public static Intent getIntent(Context context)
    {
        return new Intent(context, BrowseActivity.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void onDestroy()
    {
        mPresenter.onDetach();
        super.onDestroy();
    }

    @Override
    protected void getLayout() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_browse);
    }

    @Override
    protected void initViewAndData() {
        mPresenter.onAttach(this);

        setToolbar(mBinding.toolBar.toolBar,true);

        initRefreshLayout();

        initRecyclerView();

        onRefresh(null);
    }

    private void initRefreshLayout() {
        SmartRefreshUtils.initRefresh(mContext,mBinding.refreshLayout,this);
    }

    private void initRecyclerView()
    {
        mBinding.recyclerView.openEmptyView((view, state) -> {
            if (state == JvtdEmptyView.STATE_ERROR)
                onRefresh(null);
        });
        mBinding.recyclerView.setRefreshLayout(mBinding.refreshLayout);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mPresenter.getMyBrowseList();
    }

    @Override
    public void getMyBrowseListSuccess(List<BrowseBean> myBrowseBeans) {
        mBinding.recyclerView.setData(myBrowseBeans);
    }

    @Override
    public void getMyBrowseListFailed() {
        mBinding.recyclerView.firstLoadFailed();
    }
}
